package com.hkts.naturalgas.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.datahandler.utils.GetRandomUtils;
import com.hkts.naturalgas.dictionaries.utils.DBUtils;
import com.hkts.naturalgas.summary.entity.SummaryDictionary;
import com.hkts.naturalgas.summary.mapper.SummaryDictionaryMapper;
import com.hkts.naturalgas.summary.service.ISummaryDictionaryService;
import com.hkts.naturalgas.summary.service.serviceimpl.SummaryDictionaryServiceImpl;
import com.hkts.naturalgas.tcp.StrUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassNameTestDataUtils
 * @Description
 * @Author yck
 * @Date2020-09-17 14:40
 * @Version V1.0
 **/
@Slf4j
public class TestDataUtils {

    private ISummaryDictionaryService summaryDictionaryMapper=new SummaryDictionaryServiceImpl();

    public DBUtils dBUtils=new DBUtils();
    //获取一段时间所有日期
    public static void main(String[] args) throws ParseException {
    String msg="##1294QN=20220207160212193;ST=31;CN=2051;PW=123456;MN=ZG130121201631;Flag=5;CP=&&DataTime=20220207155800;a00000-Cou=462.3,a00000-Min=6.693,a00000-Avg=7.705,a00000-Max=8.995,a00000-Flag=N;a01011-Min=3.139,a01011-Avg=3.613,a01011-Max=4.219,a01011-Flag=N;a01012-Min=57.049,a01012-Avg=57.085,a01012-Max=57.103,a01012-Flag=N;a01013-Min=75.956,a01013-Avg=82.896,a01013-Max=97.206,a01013-Flag=N;a01014-Min=17.926,a01014-Avg=17.954,a01014-Max=17.973,a01014-Flag=N;a19001-Min=15.922,a19001-Avg=15.934,a19001-Max=15.95,a19001-Flag=N;a21001-Cou=0.002,a21001-Min=1.96,a21001-Avg=3.299,a21001-Max=4.874,a21001-Flag=N;a21001-ZsCou=0.002,a21001-ZsMin=1.96,a21001-ZsAvg=3.299,a21001-ZsMax=4.874,a21001-ZsFlag=N;a21002-Cou=0.012189,a21002-Min=25.007726,a21002-Avg=26.365203,a21002-Max=26.996292,a21002-Flag=N;a21002-ZsCou=0.012189,a21002-ZsMin=25.007726,a21002-ZsAvg=26.365203,a21002-ZsMax=26.996292,a21002-ZsFlag=N;a21026-Cou=0.01607,a21026-Min=33.745163,a21026-Avg=34.761259,a21026-Max=35.526474,a21026-Flag=N;a21026-ZsCou=0.01607,a21026-ZsMin=33.745163,a21026-ZsAvg=34.761259,a21026-ZsMax=35.526474,a21026-ZsFlag=N;a34013-Cou=0.000936,a34013-Min=1.694998,a34013-Avg=2.025153,a34013-Max=2.309117,a34013-Flag=N;a34013-ZsCou=0.000936,a34013-ZsMin=1.694998,a34013-ZsAvg=2.025153,a34013-ZsMax=2.309117,a34013-ZsFlag=N&&2200";
        String[] split = msg.toString().split(";");
        String mn = "";
        String time = "";
        String so2 = "";
        String nox = "";
        for (int i = 0; i < split.length; i++) {
            if (split[i].contains("MN")) {
                mn = StrUtils.splitMN(split[i]);
            } else if (split[i].contains("DataTime")) {
                time = StrUtils.splitDataTime(split[i]);
            } else if (split[i].contains("a21026-Cou")) {
                so2 = StrUtils.splitSo2orNox(split[i], "a21026-Cou");
            } else if (split[i].contains("a21002-Cou")) {
                nox = StrUtils.splitSo2orNox(split[i], "a21002-Cou");
            }
        }
        log.info("mn:{}", mn);
        log.info("time:{}", time);
        log.info("so2:{}", so2);
        log.info("nox:{}", nox);
        if (StringUtils.isNotBlank(mn)) {
            QueryWrapper<SummaryDictionary> query = Wrappers.query();
            query.lambda().eq(SummaryDictionary::getMn, mn);
          /*  SummaryDictionary summaryDictionary = summaryDictionaryMapper.selectOne(query);


            log.info(summaryDictionary.toString());
            String summaryInsertSql = dBUtils.getSummaryInsertSql(summaryDictionary, time, so2, nox);
            log.info("汇总数据插入sql:{}", summaryInsertSql);
            List<String> sqlList = new ArrayList<>();
            sqlList.add(summaryInsertSql);
            dBUtils.updateBatch(sqlList);*/
        }
    }
    public static List<String> getDaysByTime(String[] dateArray) {
        List<String> list = new ArrayList<>();
        String[] dateBegs = dateArray[0].split("-");
        String[] dateEnds = dateArray[1].split("-");
        Calendar start = Calendar.getInstance();
        start.set(Integer.valueOf(dateBegs[0]), Integer.valueOf(dateBegs[1]) - 1, Integer.valueOf(dateBegs[2]));
        Long startTIme = start.getTimeInMillis();
        Calendar end = Calendar.getInstance();
        end.set(Integer.valueOf(dateEnds[0]), Integer.valueOf(dateEnds[1]) - 1, Integer.valueOf(dateEnds[2]));
        Long endTime = end.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24L;
        Long time = startTIme;
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            time += oneDay;
            list.add(df.format(d));
        }
        return list;
    }
    // 获取流量压力数据
    public static float getHoursData() {
        float value = 0;
        Random rand = new Random();
        for (int i = 0; i < 10; i++) {
            value = rand.nextInt(100) + 1;
        }
        return value;
    }
    //获取近7天或近30天
    public static List<String> getDateArray(int value) {
        SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < value; i++) {
            list.add(ss.format(getYesterday(value - i)));
        }
        return list;
    }
    //获取日期
    public static Date getYesterday(int i) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -i);
        Date d = cal.getTime();
        return d;
    }
    //获取昨天
    public static Date getYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date d = cal.getTime();
        return d;
    }
   //获取某一天的压力值
    public static float getDateData() {
        float value = 0;
        Random rand = new Random();
        for (int i = 0; i < 10; i++) {
            value = rand.nextInt(100) + 1;
        }
        return value * 24;
    }
    //获取一天24小时数组
    public static List<String> getListHours(String date) {
        List<String> list = new ArrayList();
        for (int i = 0; i < 24; i++) {
            list.add(date + " " + i + ":00");
        }
        return list;
    }

    //排序
    public static List<Map<String, Object>> sortLiet(List<Map<String, Object>> list) {
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Float totalGateValue = Float.parseFloat(o1.get("totalGateValue").toString());
                Float totalGateValue2 = Float.parseFloat(o2.get("totalGateValue").toString());
                return -totalGateValue.compareTo(totalGateValue2);
            }
        });

        return list;
    }

    //获取当前
    //获取今天时间
    public static List<String> getTodayHours() {
        List<String> list = new ArrayList<String>();
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        for (int i = 0; i <= hour; i++) {
            list.add(dd.format(new Date()) + " " + i + ":00");
        }
        return list;
    }

    //报警信息转换
    public static String getGardeName(String grade) {
        if (grade.equals("1")) {
            return "一般";
        } else if (grade.equals("2")) {
            return "危险";
        } else {
            return "高危";
        }
    }
    //温度值
    public static float getTemperatureData() {
        float value = 0;
        Random rand = new Random();
        for (int i = 0; i < 10; i++) {
            value = rand.nextInt(60) + 1;
        }
        return value ;
    }
    //累积值
    public static float getAccumulateData() {
        float value = 0;
        Random rand = new Random();
        for (int i = 0; i < 10; i++) {
            value = rand.nextInt(100000) + 100000;
        }
        return value ;
    }
    //瞬时值
    // 获取流量压力数据
    public static float getInstantaneousData() {
        float value = 0;
        Random rand = new Random();
        for (int i = 0; i < 10; i++) {
            value = rand.nextInt(20) +160;
        }
        return value;
    }
    //测试pool
    public static String getUrl(){
        Random rand = new Random();
        float value =rand.nextInt(20);
        if(value>10){
            return "http://192.168.204.21:90//biz";
        }
        return "http://192.168.204.21:93//biz";
    }

    public static int daysBetween(Date smdate,Date bdate) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    public static String getStart(String date) throws ParseException {
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = formater.parse(date);
        return formater.format(parse) + " 00:00:00";

    }
    public static String getEnd(String date) throws ParseException {
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = formater.parse(date);
        return formater.format(parse) + " 23:59:59";
    }

}
